<!DOCTYPE html>
<html lang="zh_cn">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>发货管理</title>
	<link rel="stylesheet" href="./css/normalize.css">
	<link rel="stylesheet" href="./css/style.css">
</head>
<body>
	<div id="loading"></div>

	<div id="wrap">
		<div class="searchArea">
			<div class="cont">
				<div class="search">
					<input type="text" name="" id="searchKey" class="inputSearch">
					<button class="btnSearch"></button>
					<button class="addNew">添加</button>
				</div>
				<div class="more">
					<p><span>创建开始时间:</span><input type="date" name="searchDateStart" id="searchDateStart"></p>
					<p><span>创建结束时间:</span><input type="date" name="searchDateEnd" id="searchDateEnd"></p>
				</div>
				<p class="moreToggle">
					<span class="icon rotate"></span><span class="txt">显示</span>高级过滤
				</p>
			</div>
		</div>

		<div class="listTabBar">
			<ul class="clearfix">
				<li class="on">未收货 <span id="totalCount1"></span></li>
				<li>已收货 <span id="totalCount2"></span></li>
			</ul>
		</div>

		<div class="listArea notReceiveListArea"></div>
		<div class="listArea receiveListArea displaynone"></div>
	</div>

	<script src="./js/jquery-1.12.4.min.js"></script>
	<script src="./js/common.js"></script>
	<script src="./js/custom.js"></script>
	<script>
		var cookie = window.location.href.split('?')[1];

		if (cookie) {
				var accessToken = cookie.split('=')[1];
				localStorage.setItem('accessToken', accessToken);

		}

		var _loadObject = {
			notReceive: false,
			receive: false
		}
		var _loadFlag = true;
		var _notReceiveSkip = 0;
		var _notReceiveFlag = true;
		var _notReceiveUl = $('<ul></ul>').appendTo($('#wrap .notReceiveListArea'));
		var _receiveSkip = 0;
		var _receiveFlag = true;
		var _receiveUl = $('<ul></ul>').appendTo($('#wrap .receiveListArea'));

		function notReceiveLoad(){
			_loadObject.notReceive = false;
			_notReceiveFlag = false;
			var postData = {
				StartDate: fmt($('#searchDateStart').val(),'pdate1'),
				EndDate: fmt($('#searchDateEnd').val(),'pdate2'),
				State: '未收货',
				Filter: $('#searchKey').val(),
				MaxResultCount: 10,
				SkipCount: _notReceiveSkip,
				isSupplier: true,
			};

			loadShow();
			$.ajax({
				url:"https://mom.51xietong.cn/appsms_api/api/services/app/DeliveryMain/GetAllDeliveryMain",
				type: "GET",
				headers: {
					Authorization: 'Bearer '+localStorage.getItem('accessToken')
				},
				dataType: "json",
				data: postData,
				complete: function (){
					_loadObject.notReceive = true;
					_loadFlag = true;
					for ( i in _loadObject ) {
						if ( _loadObject[i] == false ) {
							_loadFlag = false;
						}
					}
					if ( _loadFlag ) { loadHide(); }
				},
				success: function (data) {
					$('#totalCount1').text('('+data.result.totalCount+')');

					if(data && data.result && data.result.items.length && data.result instanceof Object){
						var item = data.result;

						for ( i in item.items ) {
							var ival = item.items[i];
							var aLi = $('<li></li>').addClass('list').appendTo(_notReceiveUl);
							var info = $('<div></div>').addClass('info').appendTo(aLi);

							info.append($('<div></div>').addClass('icon rotate'));
							info.append($('<p></p>').text(fmt(ival.logisticsCode)).prepend($('<span></span>').text('物流单号:')));
							info.append($('<p></p>').text(fmt(ival.orderCode)).prepend($('<span></span>').text('订单号:')));
							info.append($('<p></p>').text(fmt(ival.creationTime,'datetime')).prepend($('<span></span>').text('创建时间:')));
							info.append($('<p></p>').addClass('over').text(fmt(ival.remarks)).prepend($('<span></span>').text('备注:')));
							info.append($('<button></button>').addClass('btn btnModify').text('修改').data('id',fmt(ival.id)));
							info.append($('<button></button>').addClass('btn btnDelete').text('删除').data('id',fmt(ival.id)));

							var moreInfo = $('<div></div>').addClass('moreInfo').appendTo(aLi);
							var cont = $('<div></div>').addClass('cont').appendTo(moreInfo);
							cont.append($('<div></div>').addClass('title').text('交货单详情'));

							var cUi = $('<ul></ul>').appendTo(cont);
							for (j=0;j<ival.deliveries.length;j++){
								var jval = ival.deliveries[j];
								var cLi = $('<li></li>').appendTo(cUi);
								cLi.append($('<p></p>').text(fmt(jval.taskName)).prepend($('<span></span>').text('产品名称:')));
								cLi.append($('<p></p>').text(fmt(jval.type)).prepend($('<span></span>').text('产品类型:')));
								cLi.append($('<p></p>').text(fmt(jval.modelCode)).prepend($('<span></span>').text('规格:')));
								cLi.append($('<p></p>').text(fmt(jval.picNo)).prepend($('<span></span>').text('图号:')));
								cLi.append($('<p></p>').text(fmt(jval.deliveryDate,'date')).prepend($('<span></span>').text('发货日期:')));
								cLi.append($('<p></p>').text(fmt(jval.receivingDate,'date')).prepend($('<span></span>').text('收货日期:')));
								cLi.append($('<p></p>').text(fmt(jval.deliveryAmount)).prepend($('<span></span>').text('发货数量:')));
								cLi.append($('<p></p>').text(fmt(jval.receivingAmount)).prepend($('<span></span>').text('收货数量:')));
								cLi.append($('<p></p>').text(fmt(jval.passedAmount)).prepend($('<span></span>').text('合格数量:')));
								var rStatus;
								switch (fmt(jval.receivingStatus).toString()){
									case '-1':
										rStatus = '未验收';
										break;
									case '0':
										rStatus = '合格';
										break;
									case '1':
										rStatus = '让步接受';
										break;
									case '2':
										rStatus = '退回';
										break;
									case '3':
										rStatus = '换货';
										break;
								}
								cLi.append($('<p></p>').text(rStatus).prepend($('<span></span>').text('验收结果:')));
							}
						}

						_notReceiveSkip += 10;
						if ( _notReceiveSkip < item.totalCount ) {
							_notReceiveFlag = true;
						}
					} else if ( _notReceiveSkip == 0 ) {
						$('#wrap .notReceiveListArea > ul').append($('<li></li>').addClass('empty'));
					}
				},
				error:function(e){
					console.log(e);
				}
			});
		}

		function receiveLoad(){
			_loadObject.receive = false;
			_receiveFlag = false;
			var postData = {
				StartDate: fmt($('#searchDateStart').val(),'pdate1'),
				EndDate: fmt($('#searchDateEnd').val(),'pdate2'),
				State: '已收货',
				Filter: $('#searchKey').val(),
				MaxResultCount: 10,
				SkipCount: _receiveSkip,
				isSupplier: true,
			};

			loadShow();
			$.ajax({
				url:"https://mom.51xietong.cn/appsms_api/api/services/app/DeliveryMain/GetAllDeliveryMain",
				type: "GET",
				headers: {
					Authorization: 'Bearer '+localStorage.getItem('accessToken')
				},
				dataType: "json",
				data: postData,
				complete: function (){
					_loadObject.receive = true;
					_loadFlag = true;
					for ( i in _loadObject ) {
						if ( _loadObject[i] == false ) {
							_loadFlag = false;
						}
					}
					if ( _loadFlag ) { loadHide(); }
				},
				success: function (data) {
					$('#totalCount2').text('('+data.result.totalCount+')');

					if(data && data.result && data.result.items.length && data.result instanceof Object){
						var item = data.result;

						for ( i in item.items ) {
							var ival = item.items[i];
							var aLi = $('<li></li>').addClass('list').appendTo(_receiveUl);
							var info = $('<div></div>').addClass('info').appendTo(aLi);

							info.append($('<div></div>').addClass('icon rotate'));
							info.append($('<p></p>').text(fmt(ival.logisticsCode)).prepend($('<span></span>').text('物流单号:')));
							info.append($('<p></p>').text(fmt(ival.orderCode)).prepend($('<span></span>').text('订单号:')));
							info.append($('<p></p>').text(fmt(ival.creationTime,'datetime')).prepend($('<span></span>').text('创建时间:')));
							info.append($('<p></p>').addClass('over').text(fmt(ival.remarks)).prepend($('<span></span>').text('备注:')));

							var moreInfo = $('<div></div>').addClass('moreInfo').appendTo(aLi);
							var cont = $('<div></div>').addClass('cont').appendTo(moreInfo);
							cont.append($('<div></div>').addClass('title').text('交货单详情'));

							var cUi = $('<ul></ul>').appendTo(cont);
							for (j=0;j<ival.deliveries.length;j++){
								var jval = ival.deliveries[j];
								var cLi = $('<li></li>').appendTo(cUi);
								cLi.append($('<p></p>').text(fmt(jval.taskName)).prepend($('<span></span>').text('产品名称:')));
								cLi.append($('<p></p>').text(fmt(jval.type)).prepend($('<span></span>').text('产品类型:')));
								cLi.append($('<p></p>').text(fmt(jval.modelCode)).prepend($('<span></span>').text('规格:')));
								cLi.append($('<p></p>').text(fmt(jval.picNo)).prepend($('<span></span>').text('图号:')));
								cLi.append($('<p></p>').text(fmt(jval.deliveryDate,'date')).prepend($('<span></span>').text('发货日期:')));
								cLi.append($('<p></p>').text(fmt(jval.receivingDate,'date')).prepend($('<span></span>').text('收货日期:')));
								cLi.append($('<p></p>').text(fmt(jval.deliveryAmount)).prepend($('<span></span>').text('发货数量:')));
								cLi.append($('<p></p>').text(fmt(jval.receivingAmount)).prepend($('<span></span>').text('收货数量:')));
								cLi.append($('<p></p>').text(fmt(jval.passedAmount)).prepend($('<span></span>').text('合格数量:')));
								var rStatus;
								switch (fmt(jval.receivingStatus).toString()){
									case '-1':
										rStatus = '未验收';
										break;
									case '0':
										rStatus = '合格';
										break;
									case '1':
										rStatus = '让步接受';
										break;
									case '2':
										rStatus = '退回';
										break;
									case '3':
										rStatus = '换货';
										break;
								}
								cLi.append($('<p></p>').text(rStatus).prepend($('<span></span>').text('验收结果:')));
							}
						}

						_receiveSkip += 10;
						if ( _receiveSkip < item.totalCount ) {
							_receiveFlag = true;
						}
					} else if ( _notReceiveSkip == 0 ) {
						$('#wrap .receiveListArea > ul').append($('<li></li>').addClass('empty'));
					}
				}
			});
		}

		$(window).on("scroll", function () {
			if ( $(document).scrollTop() + $(window).height() > $(document).height() - 20 ) {
				if ( $('#wrap .listTabBar .on').index() == 0 && _notReceiveFlag ) {
					notReceiveLoad();
				} else if ( $('#wrap .listTabBar .on').index() == 1 && _receiveFlag ) {
					receiveLoad();
				}
			}
		});

		$('#wrap').on('click', '.addNew', function(){
			window.location.href = "addNew.html";
		});

		$('#wrap').on('keyup', '.searchArea .search #searchKey', function(e){
			if ( e.which == 13 ) {
				$('#wrap .searchArea .search .btnSearch').click();
			}
		});

		$('#wrap').on('click', '.searchArea .search .btnSearch', function(){
			_notReceiveSkip = 0;
			_receiveSkip = 0;
			$('#wrap .listArea > ul').text('');
			notReceiveLoad();
			receiveLoad();
		});

		$('#wrap').on('click', '.searchArea .moreToggle', function(){
			_this = $(this);
			_this.children('.icon').toggleClass('on');
			var txt = _this.children('.txt').text();
			txt = txt=='隐藏' ? '显示' : '隐藏';
			_this.children('.txt').text(txt);
			$('#wrap .searchArea .more').slideToggle('fast');
		});

		$('#wrap').on('click', '.listTabBar li', function(){
			_this = $(this);
			_index = $(this).index();
			_this.addClass('on').siblings().removeClass('on');
			$('#wrap .listArea').eq(_index).removeClass('displaynone').siblings('.listArea').addClass('displaynone');
		});

		$('#wrap').on('click', '.listArea .list .info', function(){
			_this = $(this);
			_this.children('.icon').toggleClass('on');
			_this.siblings('.moreInfo').slideToggle('fast');
		});

		$('#wrap').on('click', '.listArea .list .info .btnModify', function(){
			window.location.href = './modify.html?id='+$(this).data('id');
		});

		$('#wrap').on('click', '.listArea .list .info .btnDelete', function(e){
			e.stopPropagation();
			var _this = $(this);

			confirm('确定要删除当前发货单吗？','是',function(){
				loadShow();
				$.ajax({
					url:"https://mom.51xietong.cn/appsms_api/api/services/app/DeliveryMain/DeleteDeliveryMain",
					type: "DELETE",
					headers: {
						Authorization: 'Bearer '+localStorage.getItem('accessToken')
					},
					data: {
						id: _this.data('id')
					},
					complete: function (){
						loadHide();
					},
					success: function(data){
						// alert('当前请求已成功处理！');
						confirm('当前请求已成功处理！','确定',function(){
							window.location.reload();
						});
					},
					error: function (){
						// alert('抱歉！当前请求处理失败！');
						confirm('抱歉！当前请求处理失败！','确定',function(){});
					}
				});
			},'否',function(){});
		});

		$(function(){
			notReceiveLoad();
			receiveLoad();
		});
	</script>
</body>
</html>